package com.longqin.business.service.impl;

import com.longqin.business.entity.DesDiagram;
import com.longqin.business.feign.FeignService;
import com.longqin.business.mapper.DesDiagramMapper;
import com.longqin.business.service.IDesDiagramService;
import com.longqin.business.util.OperationLog;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author longqin
 * @since 2024-11-22
 */
@Service
public class DesDiagramServiceImpl extends ServiceImpl<DesDiagramMapper, DesDiagram> implements IDesDiagramService {

	@Autowired
	DesDiagramMapper desDiagramMapper;
	
	@Autowired
	FeignService feignService;
	
	private static final String PARENT_MENU_NAME = "自定义图表";
	
	@Override
    public DesDiagram getById(int id) {
		DesDiagram desDiagram =  desDiagramMapper.selectById(id);
		return desDiagram;
    }
	
	@Override
    public List<DesDiagram> getPage(String modelName, int organizationId, int startIndex, int pageSize) {
        return desDiagramMapper.selectPage(modelName, organizationId, startIndex, pageSize);
    }
    
	@Override
    public int getCount(String modelName, int organizationId) {
        return desDiagramMapper.selectCount(modelName, organizationId);
    }
	
	@OperationLog(title = "删除图表", content = "'图表id：' + #id", operationType = "1")
	@Override
    public int delete(int id) throws Exception {
		return desDiagramMapper.delete(id);
    }
	
	@OperationLog(title = "创建图表", content = "'图表名称：' + #entity.getModelName()", operationType = "0")
	@Transactional(rollbackFor = Exception.class)
	@Override
    public int create(DesDiagram entity) throws Exception {
		int result = desDiagramMapper.insert(entity);
		if (result > 0){
			// 生成菜单
            feignService.diyMenu(entity.getModelName(), "/diydiagram/view/" + entity.getId(), entity.getId() + 2, false, PARENT_MENU_NAME);
		}
		return result;
	}
	
	@OperationLog(title = "修改图表", content = "'图表名称：' + #entity.getModelName()", operationType = "2")
	@Override
    public int update(DesDiagram entity) throws Exception {
		int result = desDiagramMapper.updateById(entity);
		if (result > 0){
			// 生成菜单
            feignService.diyMenu(entity.getModelName(), "/diydiagram/view/" + entity.getId(), entity.getId() + 2, true, PARENT_MENU_NAME);
		}
		return result;
	}
	
	@Override
	public List<DesDiagram> getByCreator(int creator){
		return desDiagramMapper.selectByCreator(creator);
	}
	
	@Override
	public String getDiagramData(String dataSource, String column, int organizationId) {
		StringBuilder sBuilder = new StringBuilder("select CONCAT(");
		sBuilder.append(column);
		sBuilder.append(", '') from ");
		sBuilder.append(dataSource);
		sBuilder.append(" where status = 1 and organization_id = ");
		sBuilder.append(organizationId);
		sBuilder.append(" order by id desc limit 1");
        return desDiagramMapper.selectDiagramData(sBuilder.toString());
    }
}
